﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web.Validation;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using Cn.Edwon.Biz;
using Cn.Cellula.Model.Content;
using Cn.Cellula.Client;
using Cn.Cellula.Model.Security;

namespace Cn.Cellula.Website.Admin.Content.Msg {
    public partial class Send : PageSupport {
        IBiz biz = BizFactory.Get<IBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.Ac.IsLogined) {
                this.btn_save.Enabled = false;
                this.txt_Reciver.Enabled = false;
                this.txt_Content.Enabled = false;
                this.txt_Title.Enabled = false;
                return;
            }
            if (!Page.IsPostBack) {
                try {
                    InitForm();
                } catch (Exception ex){
                    this.btn_save.Enabled = false;
                    this.txt_Title.Text = "初始化信息错误";
                    this.txt_Content.Text = ex.Message;
                }
            }
        }

        void InitForm() {
            String act = Request.QueryString["act"];
            if (act == "reply") {
                String replyKey = Request.QueryString["rid"];
                if (String.IsNullOrEmpty(replyKey)) return;
                VMsgEO eo = biz.Load<VMsgEO>(replyKey);
                this.txt_Title.Text = (eo.Title.IndexOf("RE:") == 0) ? eo.Title : "RE:" + eo.Title;
                if (eo.IsUnrepliable == 1) {
                    this.btn_save.Enabled = false;
                    this.txt_Content.Text = "该信息不允许回复";
                } else {
                    this.txt_Reciver.Text = eo.SenderName;
                    this.hid_Reciver.Value = eo.Sender;
                    this.btn_save.Enabled = false;
                }
            } else {
                String sendto = Request.QueryString["uid"];
                if (!String.IsNullOrEmpty(sendto)) {
                    TSysUserEO user = biz.Load<TSysUserEO>(sendto);
                    this.txt_Reciver.Text = user.UserName;
                    this.hid_Reciver.Value = user.UserKey;
                }
            }
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_Title.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            TMsgEO eo = new TMsgEO();
            eo.Title = this.txt_Title.Text;
            eo.Content = this.txt_Content.Text;
            eo.IsRead = 0;
            eo.Sender = this.Actor.ID;
            eo.SendDate = DateTime.Now;
            eo.Reciver = this.hid_Reciver.Value;
            biz.Update<TMsgEO>(eo);
            BizFactory.Get<ILogBiz>().LogUpdate("站内消息", eo.Title, this.Actor, Request);
            SiteInfo.Send(Page, "消息", "发送成功", "Admin/Content/Msg/List.aspx");
        }
    }
}
